package blackitheima2.ClassTest;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

//获取类加载器
public class MyClass {
    public static void main(String[] args) throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException, InstantiationException {
        //获取系统类加载器
        ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
        System.out.println(systemClassLoader);

        ClassLoader classLoader = MyClass.class.getClassLoader();
        System.out.println(classLoader);

        //获取平台类加载器
        ClassLoader classLoader1 = systemClassLoader.getParent();
        System.out.println(classLoader1);

        //获取内置类加载器
        ClassLoader classLoader2 = classLoader1.getParent();
        System.out.println(classLoader2);

        //调用系统类加载器的另一个方法 ,从src类下获取文件,返回字节流
        InputStream stream = systemClassLoader.getResourceAsStream("prop.properties");

        //创建properties对象获取其内部的键值
        Properties prop=new Properties();
        //将字节流导入prop对象中
        prop.load(stream);
        //通过键来寻找值
        String classname = prop.getProperty("Classname");
        String methodClass = prop.getProperty("methodClass");

        //利用反射来调用方法
        //创建Class类
        Class<?> aClass = Class.forName(classname);
        //提取成员方法
        Method study = aClass.getDeclaredMethod(methodClass);
        //暴力反射一下
        study.setAccessible(true);

        //创建对象
        Object o = aClass.newInstance();

        //调用成员方法
       study.invoke(o);


    }
}
